<template>
	<u-popup v-model="show" mode="right" :closeable="true" width="80%" border-radius="14">
		<view class="font-model-wraper">
			<view class="font-model-content">
				<view class="modal-title">图片设置</view>
				<view class="content">
					<view class="rotate">
						<text>旋转角度：</text>
						<input maxlength="3" @input="changeVal($event, 'rotate')" v-model="imgObj.rotate" type="text" />
					</view>
					<view class="rotate">
						<text>图层层级：</text>
						<input maxlength="3" @input="changeVal($event, 'zIndex')" v-model="imgObj.zIndex" type="text" />
					</view>
					<view class="rotate">
						<text>圆角大小：</text>
						<input maxlength="3" @input="changeVal($event, 'radius')" v-model="imgObj.radius" type="text" />
					</view>
					<view class="rotate">
						<text>图片宽度大小：</text>
						<input maxlength="3" @input="changeVal($event, 'width')" v-model="imgObj.width" type="text" />
					</view>
					<view class="rotate">
						<text>图片高度大小：</text>
						<input maxlength="3" @input="changeVal($event, 'height')" v-model="imgObj.height" type="text" />
					</view>
					<view class="action-box">
						<view class="title">背景图</view>
						<view class="btn-box">
							<view class="confirm" @tap="choose">选择背景图</view>
							<!-- <view class="confirm del" v-if="imgSrc" @tap="del">删除</view> -->
						</view>
					</view>
					<view class="img-box">
						<image v-if="imgSrc" mode="aspectFit" :src="imgSrc"></image>
					</view>
				</view>
				<view class="btn-wrap">
					<view class="btn cancel" @click="handleCancel">取消</view>
					<view class="btn confirm" @click="handleConfirm">确认</view>
				</view>
				<view></view>
			</view>
		</view>
		<s-color ref="color" :color="color" @confirm="handleColorConfirm" />
	</u-popup>
</template>

<script>
	import sColor from '../s-color/s-color.vue';
	import uPopup from '../u-popup/u-popup.vue';
	import { formatTool } from '../../utils/tool.js';
	export default {
		name: 'sImg',
		props: {
			// 是否显示弹窗
			isShow: {
				type: Boolean,
				default: false
			},
			imgObjData: {
				type: Object,
				default: () => {
					return {};
				}
			}
		},
		components: { sColor, uPopup },
		watch: {
			imgObjData: {
				handler(o) {
					this.imgSrc = o.url;
					this.imgObj = Object.assign({
							radius: 2,
							width: 50,
							height: 50,
							rotate: 0
						},
						o
					);
				},
				deep: true,
				immediate: true
			}
		},
		data() {
			return {
				show: false,
				currentType: '',
				color: '',
				imgSrc: '',
				imgObj: {
					radius: 2,
					width: 50,
					height: 50,
					rotate: 0
				}
			};
		},
		methods: {
			changeVal(e, key) {
				let val = e.target.value;
				if (!val) return;
				if (!formatTool(val, 2)) {
					this.$nextTick(() => {
						if (key == 'rotate') {
							this.imgObj[key] = 0;
						} else {
							this.imgObj[key] = 1;
						}
					})
				}
			},
			choose() {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						const path = res.tempFilePaths[0];
						let ext;
						// #ifdef H5
						ext = res.tempFiles[0].name.split('.').pop();
						// #endif
						// #ifndef H5
						// 字节跳动小程序ios端选择文件会带query
						ext = res.tempFilePaths[0].split('?')[0].split('.').pop();
						// #endif
						const options = {
							filePath: path,
							cloudPath: Date.now() + '.' + ext
						};
						this.imgSrc = path;
						this.imgObj.url = path;
					},
					fail: () => {
						reject(new Error('Fail_Cancel'));
					}
				});
			},
			change(type) {
				this.currentType = type;
				this.color = this.imgObj[type];
				this.$refs.color.show = true;
			},
			handleCancel() {
				this.show = false;
				this.imgObj = Object.assign({
						radius: 2,
						width: 50,
						height: 50,
						rotate: 0
					},
					this.imgObjData
				);
				this.imgSrc = this.imgObj.url;
				this.$emit('cancel', 'cancel');
			},
			handleConfirm() {
				this.show = false;
				this.$emit('confirm', this.imgObj);
			},
			handleColorConfirm(data) {
				this.imgObj[this.currentType] = data.color;
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import "../../scss/common.scss";

	.font-model-wraper {
		width: 100%;
		height: 100%;
		background-color: #fff;
		position: absolute;
		z-index: 1000;
		inset: 0;

		.font-model-content {
			position: relative;

			.modal-title {
				height: 90rpx;
				line-height: 90rpx;
				width: 100%;
				text-align: center;
				font-size: 32rpx;
				color: $uni-text-color;
				background: $uni-bg-color-grey;
			}

			.content {
				background: #fff;

				.setting-box {
					padding: 10rpx 20rpx;
					border-top: 1rpx solid $uni-border-color;
					border-bottom: 1rpx solid $uni-border-color;
					overflow: hidden;

					.item {
						width: 40rpx;
						height: 40rpx;
						float: left;

						&:not(:last-child) {
							margin-right: 40rpx;
						}
					}
				}

				.textarea {
					margin: 20rpx auto 0;
					padding: 20rpx;
					border: 1px solid #ddd;
					border-radius: 10rpx;
					min-height: 200rpx;
				}
			}

			.btn-wrap {
				position: fixed;
				bottom: 0;
				left: 0;
				width: 100%;

				.btn {
					width: 50%;
					height: 90rpx;
					text-align: center;
					line-height: 90rpx;
					font-size: 32rpx;
					float: left;
					border-top: 1rpx solid $uni-border-color;
					color: #5999ff;
				}

				.btn.cancel {
					width: calc(50% - 1rpx);
					border-right: 1rpx solid $uni-border-color;
					color: #666;
				}
			}
		}
	}

	.content {
		.rotate {
			padding: 20rpx;
			font-size: 28rpx;
			display: flex;
			font-weight: 700;
			justify-content: flex-start;
			align-items: center;
			border-bottom: 1px solid #f7f7f7;

			text {
				width: 240rpx;
				margin-right: 15rpx;
			}

			.color {
				border: 1px solid #ddd;
				border-radius: 8rpx;
				width: 50rpx;
				height: 50rpx;
			}
		}
	}

	.action-box {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 10rpx;
		padding: 20rpx;

		.confirm {
			width: 120rpx;
			height: 60rpx;
			line-height: 60rpx;
			text-align: center;
			background-color: $uni-color-primary;
			color: #fff;
			border-radius: 4px;
			letter-spacing: 3px;
			font-size: 27rpx;
		}

		.confirm:active {
			background-color: $uni-color-primary;
		}

		.title {
			margin: 0rpx 0 40rpx;
			font-size: 32rpx;
		}

		.confirm {
			padding: 0 15rpx;
			font-size: 27rpx;
		}

		.title {
			margin-top: 20rpx;
		}

		.btn-box {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.confirm {
				width: auto;
				text-align: center;
				padding: 0 20rpx;
				font-size: 25rpx;
			}

			.del {
				margin-left: 15rpx;
			}
		}
	}

	.img-box {
		margin: 20rpx;
		min-height: 200rpx;
		border: 1px dashed #ddd;
		text-align: center;
		border-radius: 10rpx;
	}
</style>